#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <strings.h>
#include <signal.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>

int main(){
	int sock;
	struct sockaddr_in serv_adr;

const char *sendmsg =
					"GET /DBInsert.php?id=%s HTTP/1.1\r\n"
					"Host: hufsbest.web44.net\r\n\r\n\r\n";
			char msgbox[BUFSIZ+1];
			int sendres, tmpres;

//create socket
	sock=socket(PF_INET, SOCK_STREAM, 0);
	if(sock==-1){
		printf("socket() error");
		exit(0);
	}

	memset(&serv_adr, 0, sizeof(serv_adr));
	serv_adr.sin_family=AF_INET;
	serv_adr.sin_addr.s_addr = inet_addr("31.170.160.74");
	serv_adr.sin_port=htons(80);

	if(connect(sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr))==-1){
		printf("connect() error!");
		exit(1);
	}
	else
		puts("Connected!\n\n");


	while(1){
			char* sender[5]= {"20", "300", "100", "00", "6371"};
			int n;
			printf("RFID num(0~4) > ");
			scanf("%d", &n);

			sprintf(msgbox, sendmsg, sender[n]);
			msgbox[strlen(sendmsg)] = '\0';

			printf("msg : \n%s\n\n", msgbox);

			sendres = send(sock, msgbox, strlen(msgbox), 0);

			bzero(msgbox, sizeof(msgbox));

			while ((tmpres = recv(sock, msgbox, BUFSIZ, 0)) > 0) {
				msgbox[tmpres] = '\0';
				if (strstr(msgbox, "stock") != NULL){
					printf("   Stock Added!\n\n");
				}
				else if (strstr(msgbox, "delivery") != NULL){
					printf("   Delivery Added!\n\n");
				}
				else{
					printf("   Not Worked! - WebServer Error!\n\n");
				}
			}

bzero(msgbox, sizeof(msgbox));

		//create socket
			sock=socket(PF_INET, SOCK_STREAM, 0);
			if(sock==-1){
				printf("socket() error");
				exit(0);
			}

			memset(&serv_adr, 0, sizeof(serv_adr));
			serv_adr.sin_family=AF_INET;
			serv_adr.sin_addr.s_addr = inet_addr("31.170.160.74");
			serv_adr.sin_port=htons(80);

			if(connect(sock, (struct sockaddr *)&serv_adr, sizeof(serv_adr))==-1){
				printf("connect() error!");
				exit(1);
			}
		}

}
